<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Theory of Operation - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.application.theory-of-operation.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.application.theory-of-operation.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.application.quick-start.html">Zend_Application Quick Start</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.application.html">Zend_Application</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.application.examples.html">Examples</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.application.theory-of-operation" class="section"><div class="info"><h1 class="title">Theory of Operation</h1></div>
    

    <p class="para">
        Getting an <acronym class="acronym">MVC</acronym> application configured and ready to dispatch has
        required an increasing amount of code as more features become
        available: setting up the database, configuring your view and view
        helpers, configuring your layouts, registering plugins, registering
        action helpers, and more.
    </p>

    <p class="para">
        Additionally, you will often want to reuse the same code to
        bootstrap your tests, a cronjob, or a service script. While it&#039;s
        possible to simply include your bootstrap script, oftentimes there
        are initializations that are environment specific – you may not need
        the <acronym class="acronym">MVC</acronym> for a cronjob, or just the DB layer for a service script.
    </p>

    <p class="para">
        <span class="classname">Zend_Application</span> aims to make this easier and to
        promote reuse by encapsulating bootstrapping into <acronym class="acronym">OOP</acronym> paradigms.
    </p>

    <p class="para">
        <span class="classname">Zend_Application</span> is broken into three realms:
    </p>

    <ul class="itemizedlist">
        <li class="listitem">
            <p class="para">
                <span class="classname">Zend_Application</span>: loads the <acronym class="acronym">PHP</acronym>
                environment, including include_paths and autoloading, and instantiates
                the requested bootstrap class.
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                <span class="classname">Zend_Application_Bootstrap</span>: provides
                interfaces for bootstrap classes.
                <span class="classname">Zend_Application_Bootstrap_Bootstrap</span> provides common
                functionality for most bootstrapping needs, including
                dependency checking algorithms and the ability to load
                bootstrap resources on demand.
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                <span class="classname">Zend_Application_Resource</span> provides an
                interface for standard bootstrapping resources that can be
                loaded on demand by a bootstrap instance, as well as several
                default resource implementations.
            </p>
        </li>
    </ul>

    <p class="para">
        Developers create a bootstrap class for their application, extending
        <span class="classname">Zend_Application_Bootstrap_Bootstrap</span> or implementing (minimally)
        <span class="classname">Zend_Application_Bootstrap_Bootstrapper</span>. The entry point
        (e.g., <var class="filename">public/index.php</var>) will load
        <span class="classname">Zend_Application</span>, and instantiate it by passing:
    </p>

    <ul class="itemizedlist">
        <li class="listitem">
            <p class="para">
                The current environment
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                Options for bootstrapping
            </p>
        </li>
    </ul>

    <p class="para">
        The bootstrap options include the path to the file containing the
        bootstrap class and optionally:
    </p>

    <ul class="itemizedlist">
        <li class="listitem">
            <p class="para">
                Any extra include_paths to set
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                Any additional autoloader namespaces to register
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                Any <var class="filename">php.ini</var> settings to initialize
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                The class name for the bootstrap class (if not &quot;Bootstrap&quot;)
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                Resource prefix to path pairs to use
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                Any resources to use (by class name or short name)
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                Additional path to a configuration file to load
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                Additional configuration options
            </p>
        </li>
    </ul>

    <p class="para">
        Options may be an array, a <span class="classname">Zend_Config</span> object, or the path
        to a configuration file.
    </p>

    <div class="section" id="zend.application.theory-of-operation.bootstrap"><div class="info"><h1 class="title">Bootstrapping</h1></div>
        

        <p class="para">
            <span class="classname">Zend_Application</span>&#039;s second area of responsibility is
            executing the application bootstrap. Bootstraps minimally need to
            implement <span class="classname">Zend_Application_Bootstrap_Bootstrapper</span>,
            which defines the following <acronym class="acronym">API</acronym>:
        </p>

        <pre class="programlisting brush: php">
interface Zend_Application_Bootstrap_Bootstrapper
{
    public function __construct($application);
    public function setOptions(array $options);
    public function getApplication();
    public function getEnvironment();
    public function getClassResources();
    public function getClassResourceNames();
    public function bootstrap($resource = null);
    public function run();
}
</pre>


        <p class="para">
            This <acronym class="acronym">API</acronym> allows the bootstrap to accept the environment and
            configuration from the application object, report the resources its
            responsible for bootstrapping, and then bootstrap and run the
            application.
        </p>

        <p class="para">
            You can implement this interface on your own, extend
            <span class="classname">Zend_Application_Bootstrap_BootstrapAbstract</span>, or use
            <span class="classname">Zend_Application_Bootstrap_Bootstrap</span>.
        </p>

        <p class="para">
            Besides this functionality, there are a number of other areas of
            concern you should familiarize yourself with.
        </p>

        <div class="section" id="zend.application.theory-of-operation.bootstrap.resource-methods"><div class="info"><h1 class="title">Resource Methods</h1></div>
            

            <p class="para">
                The <span class="classname">Zend_Application_Bootstrap_BootstrapAbstract</span>
                implementation provides a simple convention for defining class
                resource methods. Any protected method beginning with a name
                prefixed with <em class="emphasis">_init</em> will be considered a resource
                method.
            </p>

            <p class="para">
                To bootstrap a single resource method, use the
                 <span class="methodname">bootstrap()</span> method, and pass it the name of the
                resource. The name will be the method name minus the
                <em class="emphasis">_init</em> prefix.
            </p>

            <p class="para">
                To bootstrap several resource methods, pass an array of names.
                Too bootstrap all resource methods, pass nothing.
            </p>

            <p class="para">
                Take the following bootstrap class:
            </p>

            <pre class="programlisting brush: php">
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initFoo()
    {
        // ...
    }

    protected function _initBar()
    {
        // ...
    }

    protected function _initBaz()
    {
        // ...
    }
}
</pre>


            <p class="para">
                To bootstrap just the  <span class="methodname">_initFoo()</span> method, do the
                following:
            </p>

            <pre class="programlisting brush: php">
$bootstrap-&gt;bootstrap(&#039;foo&#039;);
</pre>


            <p class="para">
                To bootstrap the  <span class="methodname">_initFoo()</span> and
                 <span class="methodname">_initBar()</span> methods, do the following:
            </p>

            <pre class="programlisting brush: php">
$bootstrap-&gt;bootstrap(array(&#039;foo&#039;, &#039;bar&#039;));
</pre>


            <p class="para">
                To bootstrap all resource methods, call  <span class="methodname">bootstrap()</span>
                with no arguments:
            </p>

            <pre class="programlisting brush: php">
$bootstrap-&gt;bootstrap();
</pre>

        </div>

        <div class="section" id="zend.application.theory-of-operation.bootstrap.resource-plugins"><div class="info"><h1 class="title">Bootstraps that use resource plugins</h1></div>
            

            <p class="para">
                To make your bootstraps more re-usable, we have provided the
                ability to push your resources into resource plugin classes.
                This allows you to mix and match resources simply via
                configuration. We will cover <a href="zend.application.theory-of-operation.html#zend.application.theory-of-operation.resources" class="link">how
                    to create resources</a> later; in
                this section we will show you how to utilize them only.
            </p>

            <p class="para">
                If your bootstrap should be capable of using resource plugins,
                you will need to implement an additional interface,
                <span class="classname">Zend_Application_Bootstrap_ResourceBootstrapper</span>.
                This interface defines an <acronym class="acronym">API</acronym> for locating, registering, and
                loading resource plugins:
            </p>

        <pre class="programlisting brush: php">
interface Zend_Application_Bootstrap_ResourceBootstrapper
{
    public function registerPluginResource($resource, $options = null);
    public function unregisterPluginResource($resource);
    public function hasPluginResource($resource);
    public function getPluginResource($resource);
    public function getPluginResources();
    public function getPluginResourceNames();
    public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader);
    public function getPluginLoader();
}
</pre>


            <p class="para">
                Resource plugins basically provide the ability to create
                resource intializers that can be re-used between applications.
                This allows you to keep your actual bootstrap relatively clean,
                and to introduce new resources without needing to touch your
                bootstrap itself.
            </p>

            <p class="para">
                <span class="classname">Zend_Application_Bootstrap_BootstrapAbstract</span> (and
                <span class="classname">Zend_Application_Bootstrap_Bootstrap</span> by extension)
                implement this interface as well, allowing you to utilize
                resource plugins.
            </p>

            <p class="para">
                To utilize resource plugins, you must specify them in the
                options passed to the application object and/or bootstrap. These
                options may come from a configuration file, or be passed in
                manually. Options will be of key to options pairs, with the key
                representing the resource name. The resource name will be the
                segment following the class prefix. For example, the resources
                shipped with Zend Framework have the class prefix
                &quot;<span class="classname">Zend_Application_Resource_</span>&quot;; anything following this would
                be the name of the resource. As an example,
            </p>

            <pre class="programlisting brush: php">
$application = new Zend_Application(APPLICATION_ENV, array(
    &#039;resources&#039; =&gt; array(
        &#039;FrontController&#039; =&gt; array(
            &#039;controllerDirectory&#039; =&gt; APPLICATION_PATH . &#039;/controllers&#039;,
        ),
    ),
));
</pre>


            <p class="para">
                This indicates that the &quot;FrontController&quot; resource should be
                used, with the options specified.
            </p>

            <p class="para">
                If you begin writing your own resource plugins, or utilize
                third-party resource plugins, you will need to tell your
                bootstrap where to look for them. Internally, the bootstrap
                utilizes <span class="classname">Zend_Loader_PluginLoader</span>, so you will only
                need to indicate the common class prefix an path pairs.
            </p>

            <p class="para">
                As an example, let&#039;s assume you have custom resource plugins in
                <var class="filename">APPLICATION_PATH/resources/</var> and that they share the
                common class prefix of <span class="classname">My_Resource</span>. You would then
                pass that information to the application object as follows:
            </p>

            <pre class="programlisting brush: php">
$application = new Zend_Application(APPLICATION_ENV, array(
    &#039;pluginPaths&#039; =&gt; array(
        &#039;My_Resource&#039; =&gt; APPLICATION_PATH . &#039;/resources/&#039;,
    ),
    &#039;resources&#039; =&gt; array(
        &#039;FrontController&#039; =&gt; array(
            &#039;controllerDirectory&#039; =&gt; APPLICATION_PATH . &#039;/controllers&#039;,
        ),
    ),
));
</pre>


            <p class="para">
                You would now be able to use resources from that directory.
            </p>

            <p class="para">
                Just like resource methods, you use the  <span class="methodname">bootstrap()</span>
                method to execute resource plugins. Just like with resource
                methods, you can specify either a single resource plugin,
                multiple plugins (via an array), or all plugins. Additionally,
                you can mix and match to execute resource methods as well.
            </p>

            <pre class="programlisting brush: php">
// Execute one:
$bootstrap-&gt;bootstrap(&#039;FrontController&#039;);

// Execute several:
$bootstrap-&gt;bootstrap(array(&#039;FrontController&#039;, &#039;Foo&#039;));

// Execute all resource methods and plugins:
$bootstrap-&gt;bootstrap();
</pre>

        </div>

        <div class="section" id="zend.application.theory-of-operation.bootstrap.registry"><div class="info"><h1 class="title">Resource Registry</h1></div>
            

            <p class="para">
                Many, if not all, of your resource methods or plugins will
                initialize objects, and in many cases, these objects will be
                needed elsewhere in your application. How can you access them?
            </p>

            <p class="para">
                <span class="classname">Zend_Application_Bootstrap_BootstrapAbstract</span>
                provides a local registry for these objects. To store your
                objects in them, you simply return them from your resources.
            </p>

            <p class="para">
                For maximum flexibility, this registry is referred to as a
                &quot;container&quot; internally; its only requirements are that it is an
                object. Resources are then registered as properties named after
                the resource name. By default, an instance of
                <span class="classname">Zend_Registry</span> is used, but you may also specify any
                other object you wish. The methods  <span class="methodname">setContainer()</span>
                and  <span class="methodname">getContainer()</span> may be used to manipulate the
                container itself.  <span class="methodname">getResource($resource)</span> can be
                used to fetch a given resource from the container, and
                 <span class="methodname">hasResource($resource)</span> to check if the resource has
                actually been registered.
            </p>

            <p class="para">
                As an example, consider a basic view resource:
            </p>

            <pre class="programlisting brush: php">
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initView()
    {
        $view = new Zend_View();
        // more initialization...

        return $view;
    }
}
</pre>


            <p class="para">
                You can then check for it and/or fetch it as follows:
            </p>

            <pre class="programlisting brush: php">
// Using the has/getResource() pair:
if ($bootstrap-&gt;hasResource(&#039;view&#039;)) {
    $view = $bootstrap-&gt;getResource(&#039;view&#039;);
}

// Via the container:
$container = $bootstrap-&gt;getContainer();
if (isset($container-&gt;view)) {
    $view = $container-&gt;view;
}
</pre>


            <p class="para">
                Please note that the registry and also the container is not global. This
                means that you need access to the bootstrap in order to fetch
                resources. <span class="classname">Zend_Application_Bootstrap_Bootstrap</span>
                provides some convenience for this: during its
                 <span class="methodname">run()</span> execution, it registers itself as the front
                controller parameter &quot;bootstrap&quot;, which allows you to fetch it
                from the router, dispatcher, plugins, and action controllers.
            </p>

            <p class="para">
                As an example, if you wanted access to the view resource from
                above within your action controller, you could do the following:
            </p>

            <pre class="programlisting brush: php">
class FooController extends Zend_Controller_Action
{
    public function init()
    {
        $bootstrap = $this-&gt;getInvokeArg(&#039;bootstrap&#039;);
        $view = $bootstrap-&gt;getResource(&#039;view&#039;);
        // ...
    }
}
</pre>

        </div>

        <div class="section" id="zend.application.theory-of-operation.bootstrap.dependency-tracking"><div class="info"><h1 class="title">Dependency Tracking</h1></div>
            

            <p class="para">
                In addition to executing resource methods and plugins, it&#039;s
                necessary to ensure that these are executed once and once
                only; these are meant to bootstrap an application, and
                executing multiple times can lead to resource overhead.
            </p>

            <p class="para">
                At the same time, some resources may depend on other
                resources being executed. To solve these two issues,
                <span class="classname">Zend_Application_Bootstrap_BootstrapAbstract</span>
                provides a simple, effective mechanism for dependency
                tracking.
            </p>

            <p class="para">
                As noted previously, all resources -- whether methods or plugins
                -- are bootstrapped by calling  <span class="methodname">bootstrap($resource)</span>,
                where <var class="varname">$resource</var> is the name of a resource, an array
                of resources, or, left empty, indicates all resources should be
                run.
            </p>

            <p class="para">
                If a resource depends on another resource, it should call
                 <span class="methodname">bootstrap()</span> within its code to ensure that resource
                has been executed. Subsequent calls to it will then be ignored.
            </p>

            <p class="para">
                In a resource method, such a call would look like this:
            </p>

            <pre class="programlisting brush: php">
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initRequest()
    {
        // Ensure the front controller is initialized
        $this-&gt;bootstrap(&#039;FrontController&#039;);

        // Retrieve the front controller from the bootstrap registry
        $front = $this-&gt;getResource(&#039;FrontController&#039;);

        $request = new Zend_Controller_Request_Http();
        $request-&gt;setBaseUrl(&#039;/foo&#039;);
        $front-&gt;setRequest($request);

        // Ensure the request is stored in the bootstrap registry
        return $request;
    }
}
</pre>

        </div>
    </div>

    <div class="section" id="zend.application.theory-of-operation.resources"><div class="info"><h1 class="title">Resource Plugins</h1></div>
        

        <p class="para">
            <a href="zend.application.theory-of-operation.html#zend.application.theory-of-operation.bootstrap.resource-plugins" class="link">As noted
                previously</a>, a good way to create re-usable bootstrap resources and to
            offload much of your coding to discrete classes is to utilize resource
            plugins. While Zend Framework ships with a number of standard
            resource plugins, the intention is that developers should write
            their own to encapsulate their own initialization needs.
        </p>

        <p class="para">
            Resource plugins need only implement
            <span class="classname">Zend_Application_Resource_Resource</span>, or, more simply
            still, extend
            <span class="classname">Zend_Application_Resource_ResourceAbstract</span>. The basic
            interface is simply this:
        </p>

        <pre class="programlisting brush: php">
interface Zend_Application_Resource_Resource
{
    public function __construct($options = null);
    public function setBootstrap(
        Zend_Application_Bootstrap_Bootstrapper $bootstrap
    );
    public function getBootstrap();
    public function setOptions(array $options);
    public function getOptions();
    public function init();
}
</pre>


        <p class="para">
            The interface defines simply that a resource plugin should accept options
            to the constructor, have mechanisms for setting and retrieving
            options, have mechanisms for setting and retrieving the bootstrap
            object, and an initialization method.
        </p>

        <p class="para">
            As an example, let&#039;s assume you have a common view intialization you
            use in your applications. You have a common doctype, <acronym class="acronym">CSS</acronym> and
            JavaScript, and you want to be able to pass in a base document title
            via configuration. Such a resource plugin might look like this:
        </p>

        <pre class="programlisting brush: php">
class My_Resource_View extends Zend_Application_Resource_ResourceAbstract
{
    protected $_view;

    public function init()
    {
        // Return view so bootstrap will store it in the registry
        return $this-&gt;getView();
    }

    public function getView()
    {
        if (null === $this-&gt;_view) {
            $options = $this-&gt;getOptions();
            $title   = &#039;&#039;;
            if (array_key_exists(&#039;title&#039;, $options)) {
                $title = $options[&#039;title&#039;];
                unset($options[&#039;title&#039;]);
            }

            $view = new Zend_View($options);
            $view-&gt;doctype(&#039;XHTML1_STRICT&#039;);
            $view-&gt;headTitle($title);
            $view-&gt;headLink()-&gt;appendStylesheet(&#039;/css/site.css&#039;);
            $view-&gt;headScript()-&gt;appendfile(&#039;/js/analytics.js&#039;);

            $viewRenderer =
                Zend_Controller_Action_HelperBroker::getStaticHelper(
                    &#039;ViewRenderer&#039;
                );
            $viewRenderer-&gt;setView($view);

            $this-&gt;_view = $view;
        }
        return $this-&gt;_view;
    }
}
</pre>


        <p class="para">
            As long as you register the prefix path for this resource plugin,
            you can then use it in your application. Even better, because it
            uses the plugin loader, you are effectively overriding the shipped
            &quot;View&quot; resource plugin, ensuring that your own is used instead.
        </p>
    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.application.quick-start.html">Zend_Application Quick Start</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.application.html">Zend_Application</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.application.examples.html">Examples</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="reference.html">Refer&ecirc;ncia do Zend Framework</a></li>
  <li class="header up"><a href="zend.application.html">Zend_Application</a></li>
  <li><a href="zend.application.introduction.html">Introdu&ccedil;&atilde;o</a></li>
  <li><a href="zend.application.quick-start.html">Zend_Application Quick Start</a></li>
  <li class="active"><a href="zend.application.theory-of-operation.html">Theory of Operation</a></li>
  <li><a href="zend.application.examples.html">Examples</a></li>
  <li><a href="zend.application.core-functionality.html">Funcionalidade principal</a></li>
  <li><a href="zend.application.available-resources.html">Available Resource Plugins</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>